<?php 
$req_path="";
include($req_path."includes/ini.php");
require_once($req_path."./includes/db2.php");

class M_Register_Detail extends DB2{
    private $rowCount=0;
    public $table_name="";
	
    function M_Register_Detail(){
		parent::__construct();
	
    }
    
	function getUserInfo($id,$year){
		$sql = "
				SELECT info.thai_name, s.code,
				CASE WHEN info.sex ='M' THEN 'ด.ช. ' ELSE 'ด.ญ. ' END AS title_name
				,info.thai_surname ,ifnull( info.thai_nickname,'' ) as thai_nickname
				,sy.class_id , sy.room, cl.name as class_name
				FROM tb_student_info info
				JOIN tb_student_year sy on sy.student_id = info.student_id
				JOIN tb_student s on s.student_id = info.student_id
				JOIN tb_class_level cl on cl.id = sy.class_id
				WHERE info.student_id=".$id." and sy.year=".$year;
				
		$result = $this->ExecuteData($sql);
        $this->rowCount = $result->RecordCount();
		$row = $result->FetchRow();
        return $row;
	}
   
	 
	function load_course($year,$term ,$id){
		$sql = " SELECT detail,cost, py.name cost_type	,c.id ,c.course_type
						 , ifnull(sc.course,0) as isSelect, sub.name as subject_name
				FROM tb_course  c
				JOIN tb_subject sub on sub.id = c.subject_id
				LEFT JOIN tb_student_course sc on sc.course = c.id and sc.student_id =".$id." and sc.is_cancel=0
				LEFT JOIN tb_type_payment_service py on py.id = c.`type`
				WHERE c.status='Y'  and c.year=".$year." and c.term=".$term;
		// print_r($sql);
		$result = $this->ExecuteData($sql);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	function load_teacher_list($id){
		$sql = " SELECT teacher as id , CONCAT( case  e1.title  WHEN 1 THEN 'นาย' 
						WHEN 2 THEN 'นางสาว'
						WHEN 3 THEN 'นาง'
						ELSE '' END  , ' ',e1.name ) emp_name
					 FROM tb_course c
				 	 LEFT JOIN tb_employee e1 on e1.id = c.teacher
					 WHERE c.id = ".$id." and course_type=3
					 union all
					 SELECT teacher2 , CONCAT(case  e2.title  WHEN 1 THEN 'นาย' 
											WHEN 2 THEN 'นางสาว'
											WHEN 3 THEN 'นาง'
											ELSE '' END  , ' ',  e2.name)
					 FROM tb_course c
					 LEFT JOIN tb_employee e2 on e2.id = c.teacher2
					 WHERE c.id = ".$id." and course_type=3";
		$result = $this->ExecuteData($sql);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	function load_course_detail($id){
		$sql = " SELECT id, for_class, study_date, study_time , study_time_end
		FROM tb_course_detail
		where course_id =".$id;
		
		$result = $this->ExecuteData($sql);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	function getDate($list=''){
		$sql = " SELECT value FROM tb_date WHERE id IN (".$list.")";
		$list_date ="";
		if ($list !=''){
		    $result = $this->ExecuteData($sql);
			while($row = $result->FetchRow()){
				$list_date .= $row["value"]. " - ";
			}
			$list_date = substr($list_date, 0, -2);
		}
		
		return $list_date;	
	
	}
	function getClasslist($id=''){
		$sql=" SELECT id, name
				FROM tb_class_level
				where status = 'Y'" ;
		$sql.=" AND id in (".$id.")";
		$list_class="";
		if ($id != ''){
		   $result = $this->ExecuteData($sql);
			while($row = $result->FetchRow()){
				$list_class .= $row["name"]. ", "; 
			}
			$list_class = substr($list_class, 0, -2);
		}
		return $list_class;	
	}
	function load_bus($year,$term,$id){
		$sql = "SELECT c.id,detail, cost, py.name as cost_type
						 , ifnull(sc.bus,0) as isSelect
				FROM  tb_car_manage c
				LEFT JOIN tb_student_bus sc on sc.bus = c.id and sc.student_id=".$id."  and sc.is_cancel=0
				LEFT JOIN tb_type_payment_service py on py.id = c.cost_type
				WHERE c.status='Y' and c.year=".$year." and c.term=".$term;
			//	print_r($sql);
		$result = $this->ExecuteData($sql);
        $this->rowCount = $result->RecordCount();
        return $result;
	}
	function load_bus_detail($id){
		$sql =" SELECT *
				FROM  tb_car_route
				where car_manage_id=".$id;
					
		$result = $this->ExecuteData($sql);
        $this->rowCount = $result->RecordCount();
        return $result;			 
	}
    function getRowCount(){
        
        return $this->rowCount;
        
    }	
    function insertData($val){
            if (isset($val)){
				$sqlInsert=$this->InsertSqlScript("SELECT name,status , createdate, createby FROM ". $this->table_name." where 1=2 ", $val);
				$this->ExecuteData($sqlInsert);
            }
    }
	 
    function updateData($id,$val){
            if(isset($val)){
				$sqlUpdate=$this->UpdateSqlScript("SELECT name,status , updatedate, updateby FROM ". $this->table_name." where id='".$id."'" ,$val);
				 $this->ExecuteData($sqlUpdate);
				// return $sqlUpdate;
            }
    } 
   
	 
}
?>
